using UnityEngine;
using UnityEngine.Events;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    private void Awake()
    {
        Instance = this;
    }
    public int sceneIndex = 1;
    public string nextSceneName;
    public LoadSceneManager LoadSceneManager;
    public EnemyAnger enemyAnger;
    public CountDown countDown;
    public UnityEvent<int> GameOverEvent;
    public bool IsGameOver { get; private set; } = false;


    private void Start()
    {
        GameStart();
    }

    public void GameStart()
    {
        Time.timeScale = 1;
        countDown.GameStart();
    }

    public void GamePause()
    {
        Time.timeScale = 0;
    }

    public void GameContinue()
    {
        if (IsGameOver) return;
        Time.timeScale = 1;
    }

    public void GameRestart()
    {
        LoadSceneManager.LoadScene(sceneIndex);
    }

    public void NextScene()
    {
        LoadSceneManager.LoadScene(nextSceneName);
    }

    public void BackToMenu()
    {
        LoadSceneManager.LoadScene("Select Level");
    }



    int GetStarCount()
    {
        if (enemyAnger != null)
        {
            return enemyAnger.StarCount;
        }
        return 0;
    }
    

    public void AngerFull()
    {
        if (IsGameOver) return;
        IsGameOver = true;
        Debug.Log("Game Over: AngerFull");
        GameOverEvent?.Invoke(0);
    }

    public void TimeOver()
    {
        if (IsGameOver) return;
        IsGameOver = true;
        int starCount = GetStarCount();
        Debug.Log("Game Over: TimeOver , star = " + starCount);
        GameOverEvent?.Invoke(starCount);
        PlayerPrefs.SetInt($"Level_{sceneIndex - 1}_Rating", starCount);
        PlayerPrefs.Save();
    }
}
